﻿var ProductOrder_MainStore;
var myMask;
var hidProductOrderImgID;
var imgProductOrderImgID;
var isShowScrollBar = false;
var ThisCellOleValue;
var EditCellValue;
var RowIndex;
var EditRowIndex;

Ext.onReady(function() {

    Ext.QuickTips.init();
    // 本地加载图片代码
    Ext.BLANK_IMAGE_URL = strPath + 'extjs/resources/images/default/s.gif';

    document.getElementById("td_toupi_img1").style.height = (parent.Ext.get("ContentFrame").getHeight() - 298) / 2;
    document.getElementById("td_toupi_img2").style.height = parent.Ext.get("ContentFrame").getHeight() - 298 - Ext.get("td_toupi_img1").getHeight() + 2;
    document.getElementById("img_toupi_img1").style.height = Ext.get("td_toupi_img1").getHeight() - 6;
    document.getElementById("img_toupi_img2").style.height = Ext.get("td_toupi_img2").getHeight() - 4;

    //图片窗口
    winProductOrderImg = new Ext.Window({
        width: parent.Ext.get("ContentFrame").getWidth() - 60,
        height: parent.Ext.get("ContentFrame").getHeight() - 60,
        layout: 'fit',
        plain: true,
        constrain: true, // windows不超过浏览器
        autoScroll: 'true',
        closeAction: 'hide',
        modal: true,
        deferredRender: false,
        resizable: false,
        bodyStyle: 'padding:5px;',
        buttonAlign: 'center',
        contentEl: 'divProductOrderImg',
        bodyStyle: 'border-top:0;padding:2 2 2 2;background:#E5E2EE;',
        closeAction: 'hide',
        listeners: {
            rowselect: function(sm, row, rec) {
                //选择列事件
            }
        }
    });

    myMask = new Ext.LoadMask(Ext.get("divProductOrder_Main"), { msg: "Please wait..." });
    //    myMask.show();
    // ProductOrder_Maingrid的选择列的事件
    var smProductOrder_MainModel = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        hidden: true,
        listeners: {
            rowselect: function(sm, row, rec) {
                RowIndex = row; //行的序号
            }
        }
    });

    // 设置获取生产单列表的地址
    var httpProxyProductOrder_Main = new Ext.data.HttpProxy({
        url: strPath + 'GGFW/DocumentsManage/ProductOrderCommand.fwf?Method=GetProductOrder'
    });

    // 生产单数据对应字段的读取方式
    var readerProxyProductOrder_Main = new Ext.data.JsonReader({
        root: 'Records',
        id: 'HWTransferItemID',
        totalProperty: 'totalCount',
        fields: [
        { name: 'pg_id' }
        , { name: 'kailiao_color_deep' }
        , { name: 'kailiao_color_middle' }
        , { name: 'kailiao_color_light' }
        , { name: 'kailiao_size' }
        , { name: 'paifa_id' }
        , { name: 'paifa_pf_size' }
        , { name: 'paifa_pm_size1' }
        , { name: 'paifa_pm_size2' }
        , { name: 'paifa_pm_size3' }
        , { name: 'paifa_remark' }
        , { name: 'paifa_ft_size' }
        , { name: 'juanfa_id' }
        , { name: 'juanfa_tiao_num' }
        , { name: 'juanfa_cun_num' }
        , { name: 'juanfa_tong' }
        , { name: 'juanfa_direction' }
        , { name: 'juanfa_add_num' }
        , { name: 'juanfa_remark' }
        ]
    });

    // 定义生产单数据源
    ProductOrder_MainStore = new Ext.data.Store({
        proxy: httpProxyProductOrder_Main,
        reader: readerProxyProductOrder_Main,
        remoteSort: true
    });

    //加载生产单
    ProductOrder_MainLoad();

    var ProductOrder_MainlistColumnModel = new Ext.grid.ColumnModel([
    	smProductOrder_MainModel,
	    { id: 'product_pro_id', header: "ID", align: 'center', sortable: false, hidden: true, dataIndex: 'product_pro_id' }
	    , { id: 'grid_id', header: '序', width: 30, align: 'center', sortable: false, renderer: renderGridID }
		, { id: 'kailiao_color_deep', header: "深g", width: 107, align: 'center', sortable: false, dataIndex: 'kailiao_color_deep', editor: new Ext.form.TextField({
		    listeners: {
		        focus: function(t) {
		            EditCellValue = null;
		            ThisCellOleValue = t.value;
		            EditRowIndex = RowIndex;
		        },
		        blur: function(t) {
		            CheckNumeralSlash('kailiao_color_deep', '', t.getValue(), EditRowIndex, ProductOrder_MainGridPanel, 0);
		        }
		    }
		})
		}
	    , { id: 'kailiao_color_middle', header: "中g", width: 107, align: 'center', sortable: false, dataIndex: 'kailiao_color_middle', editor: new Ext.form.TextField({
	        listeners: {
	            focus: function(t) {
	                EditCellValue = null;
	                ThisCellOleValue = t.value;
	                EditRowIndex = RowIndex;
	            },
	            blur: function(t) {
	                CheckNumeralSlash('kailiao_color_middle', '', t.getValue(), EditRowIndex, ProductOrder_MainGridPanel, 0);
	            }
	        }
	    })
	    }
        , { id: 'kailiao_color_light', header: "浅g", width: 106, align: 'center', sortable: false, dataIndex: 'kailiao_color_light', editor: new Ext.form.TextField({
            listeners: {
                focus: function(t) {
                    EditCellValue = null;
                    ThisCellOleValue = t.value;
                    EditRowIndex = RowIndex;
                },
                blur: function(t) {
                    CheckNumeralSlash('kailiao_color_light', '', t.getValue(), EditRowIndex, ProductOrder_MainGridPanel, 0);
                }
            }
        })
        }
        , { id: 'kailiao_size', header: "开料尺寸", width: 140, align: 'center', sortable: false, dataIndex: 'kailiao_size', editor: new Ext.form.TextField() }
        , { id: 'paifa_pf_size', header: "排发尺寸", width: 80, align: 'center', sortable: false, dataIndex: 'paifa_pf_size', editor: new Ext.form.TextField() }
        , { id: 'paifa_pm_size1', header: "排码尺码1", width: 80, align: 'center', sortable: false, dataIndex: 'paifa_pm_size1', editor: new Ext.form.TextField({
            listeners: {
                focus: function(t) {
                    EditCellValue = null;
                    ThisCellOleValue = t.value;
                    EditRowIndex = RowIndex;
                },
                blur: function(t) {
                    CheckNumeral('paifa_pm_size1', '', t.getValue(), EditRowIndex, ProductOrder_MainGridPanel, 0);
                }
            }
        })
        }
        , { id: 'paifa_pm_size2', header: "排码尺码2", width: 80, align: 'center', sortable: false, dataIndex: 'paifa_pm_size2', editor: new Ext.form.TextField({
            listeners: {
                focus: function(t) {
                    EditCellValue = null;
                    ThisCellOleValue = t.value;
                    EditRowIndex = RowIndex;
                },
                blur: function(t) {
                    CheckNumeral('paifa_pm_size2', '', t.getValue(), EditRowIndex, ProductOrder_MainGridPanel, 0);
                }
            }
        })
        }
        , { id: 'paifa_pm_size3', header: "排码尺码3", width: 80, align: 'center', sortable: false, dataIndex: 'paifa_pm_size3', editor: new Ext.form.TextField({
            listeners: {
                focus: function(t) {
                    EditCellValue = null;
                    ThisCellOleValue = t.value;
                    EditRowIndex = RowIndex;
                },
                blur: function(t) {
                    CheckNumeral('paifa_pm_size3', '', t.getValue(), EditRowIndex, ProductOrder_MainGridPanel, 0);
                }
            }
        })
        }
        , { id: 'paifa_remark', header: "备注", width: 130, align: 'center', sortable: false, dataIndex: 'paifa_remark', editor: new Ext.form.TextField() }
        , { id: 'paifa_ft_size', header: "发条尺寸", width: 80, align: 'center', sortable: false, dataIndex: 'paifa_ft_size', editor: new Ext.form.TextField({
            listeners: {
                focus: function(t) {
                    EditCellValue = null;
                    ThisCellOleValue = t.value;
                    EditRowIndex = RowIndex;
                },
                blur: function(t) {
                    CheckNumeral('paifa_ft_size', '', t.getValue(), EditRowIndex, ProductOrder_MainGridPanel, 0);
                }
            }
        })
        }
        , { id: 'juanfa_tiao_num', header: "条", width: 80, align: 'center', sortable: false, dataIndex: 'juanfa_tiao_num', editor: new Ext.form.TextField({
            listeners: {
                focus: function(t) {
                    EditCellValue = null;
                    ThisCellOleValue = t.value;
                    EditRowIndex = RowIndex;
                },
                blur: function(t) {
                    CheckNumeral('juanfa_tiao_num', '', t.getValue(), EditRowIndex, ProductOrder_MainGridPanel, 0);
                }
            }
        })
        }
        , { id: 'juanfa_cun_num', header: "寸", width: 80, align: 'center', sortable: false, dataIndex: 'juanfa_cun_num', editor: new Ext.form.TextField({
            listeners: {
                focus: function(t) {
                    EditCellValue = null;
                    ThisCellOleValue = t.value;
                    EditRowIndex = RowIndex;
                },
                blur: function(t) {
                    CheckNumeral('juanfa_cun_num', '', t.getValue(), EditRowIndex, ProductOrder_MainGridPanel, 0);
                }
            }
        })
        }
        , { id: 'juanfa_tong', header: "卷筒", width: 80, align: 'center', sortable: false, dataIndex: 'juanfa_tong', editor: new Ext.form.TextField() }
        , { id: 'juanfa_direction', header: "方向", width: 65, align: 'center', sortable: false, dataIndex: 'juanfa_direction', editor: new Ext.form.TextField() }
        , { id: 'juanfa_add_num', header: "条/支", width: 65, align: 'center', sortable: false, dataIndex: 'juanfa_add_num', editor: new Ext.form.TextField({
            listeners: {
                focus: function(t) {
                    EditCellValue = null;
                    ThisCellOleValue = t.value;
                    EditRowIndex = RowIndex;
                },
                blur: function(t) {
                    CheckNumeral('juanfa_add_num', '', t.getValue(), EditRowIndex, ProductOrder_MainGridPanel, 0);
                }
            }
        })
        }
        , { id: 'juanfa_remark', header: "备注", width: 132, align: 'center', sortable: false, dataIndex: 'juanfa_remark', editor: new Ext.form.TextField() }

	]);
    /**
    *描述 : 渲染列，鼠标移该单元格上面，显示完整的值
    *参数 : value：单元格的值；
    */
    function renderGridID(value, metadate, record, colIndex, rowIndex) {
        if (colIndex < 9)
            return '<div style="text-align:left;color:#969292;"><span>' + "0" + (colIndex + 1) + '</span></div>';
        else
            return '<div style="text-align:left;color:#969292;"><span>' + (colIndex + 1) + '</span></div>';
    }
    // 定义一个主GridPanel存放生产单列表信息
    ProductOrder_MainGridPanel = new Ext.grid.EditorGridPanel({
        id: "ProductOrder_MainGrid",
        height: parent.Ext.get("ContentFrame").getHeight() - 296,
        width: 1524,
        frame: false,
        loadMask: true,
        //        animCollapse: false,
        renderTo: "divProductOrder_Main",
        clicksToEdit: 1,
        stripeRows: true,
        store: ProductOrder_MainStore,
        cm: ProductOrder_MainlistColumnModel,
        sm: smProductOrder_MainModel,
        trackMouseOver: true,
        //        enableColumnHide: false,
        hideHeaders: true,
        border: false
    });

    // ProductOrder_Maingrid的选择列的事件
    var smMonitoringPositionModel = new Ext.grid.CheckboxSelectionModel({
        singleSelect: false,
        listeners: {
            rowselect: function(sm, row, rec) {
            }
        }
    });

    // 生产单信息加载
    function ProductOrder_MainLoad() {
        ProductOrder_MainStore.load({ params: { start: 0, limit: 500, pro_id: -1 }, callback: function(r) { InitBlankRow(); } });
    }

});

function InitBlankRow() {
    for (var i = 0; i < parseInt(((parent.Ext.get("ContentFrame").getHeight() - 296) - 1) / 21); i++)
        AddGridRow(false);
    ProductOrder_MainGridPanel.getSelectionModel().selectFirstRow();
}

function AddGridRow(showScrollBar) {
    ProductOrder_MainStore.add(new Ext.data.Record({ pro_id: 0 }));
    if (showScrollBar) {
        if (!isShowScrollBar) {
            isShowScrollBar = true;
            ProductOrder_MainGridPanel.getColumnModel().setColumnWidth(18, 115, true);
            ProductOrder_MainGridPanel.getView().refresh();
        }

        var c = ProductOrder_MainGridPanel.getView().scroller.dom;
        c.scrollTop = c.scrollHeight + 21;

        ProductOrder_MainGridPanel.getSelectionModel().selectLastRow();
    }
}

function btnSaveProductOrder_Click() {
    var productgridFileds = "";
    var productgridFiledsName = "";
    var productgridFiledsValue = "";
    var alertInfo = ""; //警告提示信息

    //产品主表相关信息(product)
    //var pro_client_name = Ext.get("txt_pro_client_name").dom.value;
    var pro_code = Ext.get("txt_pro_code").dom.value;
    var pro_pro_name = Ext.get("txt_pro_pro_name").dom.value;
    var pro_reviewer = Ext.get("txt_pro_reviewer").dom.value;
    var pro_designer = Ext.get("txt_pro_designer").dom.value;
    //var productFileds = "client_name,code,pro_name,reviewer,designerξψ" + pro_client_name + "ψ,ψ" + pro_code + "ψ,ψ" + pro_pro_name + "ψ,ψ" + pro_reviewer + "ψ,ψ" + pro_designer + "ψ";
    var productFileds = "code,pro_name,reviewer,designerξψ" + pro_code + "ψ,ψ" + pro_pro_name + "ψ,ψ" + pro_reviewer + "ψ,ψ" + pro_designer + "ψ";
    //产品附加表相关信息(productadd)
    var proadd_remark1 = Ext.get("txta_proadd_remark1").dom.value;
    var proadd_remark2 = Ext.get("txta_proadd_remark2").dom.value;
    var proadd_remark3 = Ext.get("txta_proadd_remark3").dom.value;
    var proadd_wendu = Ext.get("txta_proadd_wendu").dom.value;
    var proadd_date = Ext.get("txta_proadd_date").dom.value;
    var proadd_gaotou = Ext.get("txta_proadd_gaotou").dom.value;
    var proadd_zaxiang = Ext.get("txta_proadd_zaxiang").dom.value;
    var proadd_faxiang = Ext.get("txta_proadd_faxiang").dom.value;
    var proadd_fx_zheng_image = Ext.get("hid_proadd_fx_zheng_image").dom.value;
    var proadd_fx_fan_image = Ext.get("hid_proadd_fx_fan_image").dom.value;
    var proadd_toupi_guige = Ext.get("txt_proadd_toupi_guige").dom.value;
    var proadd_proadd_toupi_img1 = Ext.get("hid_proadd_toupi_img1").dom.value;
    var proadd_proadd_toupi_img2 = Ext.get("hid_proadd_toupi_img2").dom.value;
    var productaddFileds = "pro_id,remark1,remark2,remark3,wendu,date,gaotou,zaxiang,faxiang,fx_zheng_image,fx_fan_image,toupi_guige,toupi_img1,toupi_img2ξ∮pro_id∮,ψ" + proadd_remark1 + "ψ,ψ" + proadd_remark2 + "ψ,ψ" + proadd_remark3 + "ψ,ψ" + proadd_wendu + "ψ,ψ" + proadd_date + "ψ,ψ" + proadd_gaotou + "ψ,ψ" + proadd_zaxiang + "ψ,ψ" + proadd_faxiang + "ψ,ψ" + proadd_fx_zheng_image + "ψ,ψ" + proadd_fx_fan_image + "ψ,ψ" + proadd_toupi_guige + "ψ,ψ" + proadd_proadd_toupi_img1 + "ψ,ψ" + proadd_proadd_toupi_img2 + "ψ";
    //提示信息
    //if (pro_client_name == null || pro_client_name == "")
    //alertInfo += "客户信息不能为空！</br>";
    if (pro_code == null || pro_code == "")
        alertInfo += "<span style='font-weight:bold;'>编号信息</span>不能为空！</br>";
    if (pro_pro_name == null || pro_pro_name == "")
        alertInfo += "<span style='font-weight:bold;'>款名信息</span>不能为空！</br>";
    if (alertInfo != null && alertInfo != "") {
        Ext.MessageBox.alert("提示", "<span style='line-height:18px;'>" + alertInfo + "</span>");
        return;
    }
    //产品Grid表相关信息(productagrid)
    var recordArray = ProductOrder_MainStore.getModifiedRecords();
    for (var i = 0; i < recordArray.length; i++) {
        productgridFiledsName = "";
        productgridFiledsValue = "";
        items = recordArray[i].getChanges();
        var j = 0;
        for (var key in items) {
            j++;
            if (items[key] == "" || items[key] == null)
                continue;
            productgridFiledsName += key + ",";
            productgridFiledsValue += "ψ" + items[key] + "ψ" + ","
        }

        if (productgridFiledsName == "" || productgridFiledsName == null)
            continue;
        else {
            productgridFiledsName = productgridFiledsName.substring(0, productgridFiledsName.length - 1);
            productgridFiledsValue = productgridFiledsValue.substring(0, productgridFiledsValue.length - 1);
        }
        if (productgridFiledsName == "" || productgridFiledsName == null)
            continue;
        else
            productgridFileds += "pro_id," + productgridFiledsName + "ξ∮pro_id∮," + productgridFiledsValue;
        if (i == recordArray.length - 1)
            continue;
        productgridFileds += "₃";
    }

    //帽款表相关信息(maokuan)
    //var mk_maokuan = Ext.get("txt_mk_maokuan").dom.value;
    //var mk_maitou = Ext.get("txt_mk_maitou").dom.value;
    //var mk_maitou_image = Ext.get("hid_mk_maitou_image").dom.value;
    //var mk_diaopai = Ext.get("txt_mk_diaopai").dom.value;
    //var mk_diaopai_image = Ext.get("hid_mk_diaopai_image").dom.value;
    //var maokuanFileds = "pro_id,maokuan,maitou,maitou_image,diaopai,diaopai_imageξ∮pro_id∮,ψ" + mk_maokuan + "ψ,ψ" + mk_maitou + "ψ,ψ" + mk_maitou_image + "ψ,ψ" + mk_diaopai + "ψ,ψ" + mk_diaopai_image + "ψ";

    //包装材料表相关信息(baozhuang)
    var bz_fawan = Ext.get("txt_bz_fawan").dom.value;
    var bz_saizhi = Ext.get("txt_bz_saizhi").dom.value;
    var bz_zhitiao = Ext.get("txt_bz_zhitiao").dom.value;
    var bz_jiaodai = Ext.get("txt_bz_jiaodai").dom.value;
    var bz_zhixiang = Ext.get("txt_bz_zhixiang").dom.value;
    var baozhuangFileds = "pro_id,fawan,saizhi,zhitiao,jiaodai,zhixiangξ∮pro_id∮,ψ" + bz_fawan + "ψ,ψ" + bz_saizhi + "ψ,ψ" + bz_zhitiao + "ψ,ψ" + bz_jiaodai + "ψ,ψ" + bz_zhixiang + "ψ";

    //Ajax交互
    var myMask = new Ext.LoadMask(Ext.getBody(), {
        msg: "正在保存数据..."
    });
    myMask.show();

    var strUrl = strPath + 'GGFW/DocumentsManage/ProductOrderCommand.fwf?Method=SaveProductOrder'
    Ext.Ajax.request({
        url: strUrl,
        method: 'POST',
        params: {
            productFileds: productFileds
           , productaddFileds: productaddFileds
           , productgridFileds: productgridFileds
            //, maokuanFileds: maokuanFileds
           , baozhuangFileds: baozhuangFileds
        },
        failure: function(response, options) {
            myMask.hide();
            ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
        },
        success: function(response, options) {
            // 保存返回的信息
            myMask.hide();
            var json = response.responseText;
            var obj = parseJsonResult(json);

            if (!obj.Success) {
                showMessage("错误！", "保存数据出错！", obj.Message);
            }
            else {
                if (obj.Message != null && obj.Message.trim() != '') {
                    Ext.MessageBox.alert("警告", obj.Message);
                    return;
                }
                else {

                    Ext.MessageBox.confirm("询问", '保存生产单成功，是否继续添加？', function(btn) {
                        if (btn == "yes")
                            window.location.href('ProductOrderAdd.aspx');
                        else
                            window.location.href('ProductOrderList.aspx');
                    });
                }
            }
        }
    });
}

//打开图片上传窗口
function showWinProductOrderImg(imgImgID, hidImgID) {
    imgProductOrderImgID = imgImgID;
    hidProductOrderImgID = hidImgID;
    document.getElementById("imgProductOrder").src = "../UploadFile/" + document.getElementById(hidProductOrderImgID).value;
    document.getElementById(imgProductOrderImgID).src = "../UploadFile/" + document.getElementById(hidProductOrderImgID).value;
    //document.getElementById("imgProductOrder").style.width = parent.Ext.get("ContentFrame").getWidth() - 80;
    document.getElementById("imgProductOrder").style.height = parent.Ext.get("ContentFrame").getHeight() - 160;
    //document.getElementById("fileQuick").style.width = parent.Ext.get("ContentFrame").getWidth() - 180;
    winProductOrderImg.setTitle("图片上传更新");
    document.getElementById("span_info").innerHTML = "请选择图片文件（*.jpg,*.gif,*.png,*.bmp)";
    controlShow('divProductOrderImg');
    winProductOrderImg.show();
}

function CloseWinProductOrderImg() {
    winProductOrderImg.hide();
}

